package ebj.common.utils;

import java.io.IOException;
import java.io.Reader;
import java.net.URI;
import java.util.LinkedList;

public class StringUtils {
	/**
	 * 方法名     : isEmpty
	 * 描述: <p>判断参数字符串是否为空，为空则返回true，否则返回false</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceStr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-6 下午2:51:59
	 */
	public static boolean isEmpty(String sourceStr) {
		//...
		sourceStr = sourceStr.replaceAll("\\s", "");
		if (sourceStr == null || sourceStr.length() <= 0) {
			return true;
		}
		return false;
	}
	
	
	/**
	 * 方法名     : rmBlankSpace
	 * 描述: <p>去除参数字符串中的所有空格</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceStr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-6 下午2:55:42
	 */
	public static String rmBlankSpace(String sourceStr) {
		//...
		sourceStr = sourceStr.replace(" ", "");
		return sourceStr;
	}
	
	/**
	 * 方法名     : rmWhiteSpace
	 * 描述: <p>去除参数字符串当中的所有空白字符，包括回车、换行符、制表符 和 所有空格</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceStr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-6 下午2:58:18
	 */
	public static String rmWhiteSpace(String sourceStr) {
		//...
		sourceStr = sourceStr.replaceAll("\\s", "");
		return sourceStr;
	}
	
	/**
	 * 方法名     : explode
	 * 描述: <p>将参数字符串按照分隔符expr分割成数组</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceStr
	 * @param expr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-6 下午3:04:31
	 */
	public static String[] explode(String sourceStr, String expr) {
		String[] a = sourceStr.split(expr);
		return a;
	}
	
	/**
	 * 方法名     : implode
	 * 描述: <p>将字符数组的元素用分隔符连接成字符串</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceArr
	 * @param expr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-7 下午4:07:49
	 */
	public static String implode(String[] sourceArr, String expr) {
		//...
		String rsStr = "";
		StringBuffer stringBuffer = new StringBuffer();
		for (int i = 0; i < sourceArr.length; i++) {
			if (i < sourceArr.length - 1) {
				stringBuffer.append(sourceArr[i] + expr);
			}else {
				stringBuffer.append(sourceArr[i]);
			}
		}
		rsStr = stringBuffer.toString();
		return rsStr;
	}
	
	/**
	 * 方法名     : reverse
	 * 描述: <p>将参数字符串按照逆序，排列成新的字符串</p>
	 * @modify by  : 
	 * @modify date:
	 * @param sourceStr
	 * @return
	 * ADD BY zhouxy AT TIME 2013-1-7 下午4:21:15
	 */
	public String reverse(String sourceStr) {
		String rsStr = "";
		char[] chars = sourceStr.toCharArray();
		StringBuffer stringBuffer = new StringBuffer();
		
		for (int i = 0; i < chars.length; i++) {
			stringBuffer.append(chars[chars.length - i - 1]);
		}
		rsStr = stringBuffer.toString();
		return rsStr;
	}
	
	public String rmRepeat(String sourceStr) {
		StringBuffer stringBuffer = new StringBuffer();
		char[] chars = sourceStr.toCharArray();
		LinkedList<String> list1 = new LinkedList<String>();
		LinkedList<String> list2 = new LinkedList<String>();
				
		for (int i = 0; i < chars.length; i++) {
			if (list1.contains(String.valueOf(chars[i]))) {
				list2.add(String.valueOf(chars[i]));
			}else {
				list1.add(String.valueOf(chars[i]));
			}
		}
		
		for (String string : list1) {
			stringBuffer.append(string);
		}
		
		return stringBuffer.toString();
	}

    //*********add by martin @20150602*********
    public static String nullToEmpty(String string) {
		return (string == null) ? "" : string;
	}

	public static String emptyToNull(String string) {
		return isNullOrEmpty(string) ? null : string;
	}

	public static boolean isNullOrEmpty(String string) {
		return string == null || string.length() == 0; // string.isEmpty() in java 6
	}

	public static boolean isNotNullOrEmpty(String string) {
		return !isNullOrEmpty(string);
	}
	
	public static String getDomainName(URI uri) {
        return extractName(uri.getAuthority());
    }

    private static String extractName(String host) {
        if (host == null) {
            return null;
        }

        int index = host.lastIndexOf('.');
        if (index == -1) {
            //maybe IP
            return host;
        }
        host = host.substring(0, index);

        index = host.lastIndexOf('.');
        return index == -1 ? host : host.substring(index + 1);
    }

    public static String removeBlankCharacters(String message) {
        return message.replaceAll("\\s{2,}", ""); // \\s+ 表示匹配1个或者1个以上的空白字符；\\s{2,}表示匹配2个或者2个以上的空白字符
    }

    public static String removeTags(String message) {
        return message.replaceAll("<.*?>", "");
    }

    public static String removeNonLetters(String message) {
        message = message.toLowerCase();
        return message.replaceAll("[^a-zа-я]", "");
    }

    public static String toString(Reader in) throws IOException {
        final char[] buffer = new char[0x10000];
        StringBuilder out = new StringBuilder();
        int read;
        do {
            read = in.read(buffer, 0, buffer.length);
            if (read > 0) {
                out.append(buffer, 0, read);
            }
        } while (read >= 0);

        return out.toString();
    }
}
